package org.mx.graphql.service;

import graphql.schema.DataFetchingEnvironment;
import org.mx.graphql.bean.EventInfo;
import org.mx.graphql.bean.EventSummaryInfo;
import org.mx.service.rest.graphql.GraphQLAnnotationExecution;
import org.mx.service.rest.graphql.GraphQLFieldAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@SuppressWarnings("javadoc")
@Component("eventQueryExecution")
public class EventQueryExecution implements GraphQLAnnotationExecution {
    private EventQuery eventQuery;

    @Autowired
    public EventQueryExecution(EventQuery eventQuery) {
        super();
        this.eventQuery = eventQuery;
    }

    @Override
    public String getTypeName() {
        return "Query";
    }

    @GraphQLFieldAnnotation("event")
    public EventInfo event(DataFetchingEnvironment env) {
        String id = env.getArgument("id");
        return eventQuery.getEventById(id);
    }

    @GraphQLFieldAnnotation("eventSummary")
    public EventSummaryInfo eventSummary(DataFetchingEnvironment env) {
        return eventQuery.getEventSummary();
    }

    @GraphQLFieldAnnotation("similarEvents")
    public List<EventInfo> similarEvents(DataFetchingEnvironment env) {
        String content = env.getArgument("content");
        return eventQuery.getSimilarEvents(content);
    }

    @GraphQLFieldAnnotation("echo")
    public String echo(DataFetchingEnvironment env) {
        String message = env.getArgument("message");
        return "Echo: [" + message + "]";
    }
}
